

import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.jc.constant.*;
import com.jc.util.PolicyUtils;
import com.jc.util.R;
import com.jc.vo.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;


public class Demo {


    // jc提供的SecretId
    public static String SECRET_ID = "100002";
    // jc提供的SecretKey
    public static String SECRET_KEY = "2a4afaf10a0ebd926dc91387373a0594";


    public static String SERVICE_HOST = "127.0.0.1:8080";

    public static String INSURE_SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/cross/v1/createOrder";




    public static String UPDATE_SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/cross/v1/update";
    public static String CALCULATE_SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/cross/v1/calculate";
    public static String POST_MODIFY_SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/cross/v1/postModify";


    // 查询保单服务地址
    public static String QUERY_SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/cross/v1/query";
    public static String TRACKING_NO = "tesdt2as1sfs01211";
    public static String CREATE_USERNAME = "ruby";
    public static String fileUrl = "https://";


    public static void main(String[] args) {
//		System.out.println(Encryption.md5("100645" + "1685675299022" + "GQeG1OYQkUjPoZ7s"));
        System.out.println(DigestUtils.md5Hex("100001" + "20250419175859" + "a28aa7399a067919c913ead1b434552c"));
    }

    @Test
    public void testCreate() throws ParseException {


        //构建一个测试投保单
        PolicyRequest policyRequest = new PolicyRequest();


        //特色版 新增字段
        //ShelfTypeConst
        policyRequest.setShelfType("NONE");



        //设置产品编码
        //跨境大货创新类保险服务
        policyRequest.setProductCode("1");

        //被保险人
        policyRequest.setInsuredName("宁波测试有限公司");

        if (policyRequest.getProductCode().equalsIgnoreCase("1")){
            //设置原单号
            policyRequest.setTrackingNo(TRACKING_NO);
        }



        //运输方式
        policyRequest.setTransportModeCode(TransportMode.ROAD.getCode());
        //派送方式
        policyRequest.setDeliverywayCode(Deliveryway.TRUCK.getCode());
//        policyRequest.setTransportModeCode("dd");
//        policyRequest.setDeliverywayCode("sss");
        //运输工具及航次
        policyRequest.setTransportTool("LH721");
        //货物描述
        policyRequest.setCargoDesc("led灯1");
        //货物类别
        policyRequest.setCargoCategoryCode(CargoCategory.NORMAL.getCode());
        //起运国
        policyRequest.setDepartureCountryCode("CHN");
        //起运地
        policyRequest.setDepartureAddress("SHENZHEN");
        //目的国
        policyRequest.setDestinationCountryCode("USA");
        //目的地
        policyRequest.setDestinationAddress("MEM 13292 E Holmes Rd");
        // 启运日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse("2024-07-22");
        policyRequest.setDepartureDate(parse);
//        //快递公司名，如果有快递单号，则快递公司必填
//        policyRequest.setExpressCompanyCode(ExpressCompany.UPS.getCode());
        //海外派送快递单号
//        policyRequest.setExpressNo("1Z9474VX6832389526");
//        提单号
//        policyRequest.setBlNo("020-48236661");
        //包装类型 002--纸箱 CARTON
        policyRequest.setPackingCode(Packing.CARTON.getCode());
        //包装数量
        policyRequest.setPackingQuantity(5);
        //公斤数（毛重)
        policyRequest.setChargeableWeight(new BigDecimal(400));
        //货值
        policyRequest.setCargoValue(new BigDecimal(1));
        //计费方式：01--货值*加成比例
        policyRequest.setBaseAmountWay(BaseAmountWay.VALUE.getCode());
        //保单币种
        policyRequest.setCurrencyCode(Currency.USD.getCode());
        //投保比例 %
        policyRequest.setRatio(new BigDecimal(100));
        //ShipmentID
        policyRequest.setShipmentId("FBA092019293939");

        //备注
        policyRequest.setRemark("NONE");
        //是否保上架
        // true ：是
        policyRequest.setShelf(false);
        //上架目的地；
        //目前仅仅支持持FBA上架保障；
        policyRequest.setShelfName("FBA");
        //目的地类型
        policyRequest.setDestType(DestType.FBA.getCode());

        // 用户名(登录跨境堡的用户名)
        policyRequest.setCreateUserName(CREATE_USERNAME);

        policyRequest.setBlNo("sadassadadsa11221");

        // 是否暴动保障
        policyRequest.setUnrestRisk(true);

        policyRequest.setWarRisk(true);
        policyRequest.setReplyPostRisk(true);
        policyRequest.setWrongAddressRisk(false);

        //上传附件 FILE
        PolicyAttachment paq = new PolicyAttachment();
        paq.setRemake("nothing");
        paq.setType("guarantee_letter");
        paq.setUrl(fileUrl);
        List<PolicyAttachment> list = new ArrayList<>();
        list.add(paq);
        policyRequest.setAttachmentList(list);


        PolicyInsured insured = new PolicyInsured();
        insured.setCertificateType(CertificateType.PID.getCode());
        insured.setCertificateNo("330227100000000002");
        policyRequest.setInsuredInfo(insured);

        PolicyResponseVO response = null;
        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, INSURE_SERVICE_URL);
        try {
            R r = PolicyUtils.create(policyRequest, config);
            if(r.getSuccess()){
                response = (PolicyResponseVO) r.getContent();
                System.out.println(r.getMessage());
                System.out.println(response);
            }else{
                System.out.println(r.getMessage());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Test
    public void testCalculate() throws MalformedURLException, URISyntaxException {

        //构建一个测试投保单
        PolicyRequest policyRequest = new PolicyRequest();

        //设置产品编码
        //跨境大货创新类保险服务
        policyRequest.setProductCode("1");

        //运输方式
        policyRequest.setTransportModeCode(TransportMode.AIR.getCode());
        //派送方式
        policyRequest.setDeliverywayCode(Deliveryway.TRUCK.getCode());
        //起运国
        policyRequest.setDepartureCountryCode("CHN");
        //目的国
        policyRequest.setDestinationCountryCode("USA");
        //公斤数（毛重)
        policyRequest.setChargeableWeight(new BigDecimal(5438));
        //货物类别
        policyRequest.setCargoCategoryCode(CargoCategory.NORMAL.getCode());
        //货值
        policyRequest.setCargoValue(new BigDecimal(91296));
        //计费方式：01--货值*加成比例
        policyRequest.setBaseAmountWay(BaseAmountWay.VALUE_AND_FREIGHT.getCode());
        //保单币种
        policyRequest.setCurrencyCode(Currency.USD.getCode());
        //投保比例 %
        policyRequest.setRatio(new BigDecimal(100));
        //是否保上架
        // true ：是
        policyRequest.setShelf(true);
        //上架目的地；
        //目前仅仅支持持FBA上架保障；
        policyRequest.setShelfName("FBA");
        //目的地类型
        policyRequest.setDestType(DestType.FBA.getCode());

        // 用户名(登录跨境堡的用户名)
        policyRequest.setCreateUserName(CREATE_USERNAME);

        // 是否暴动保障
        policyRequest.setUnrestRisk(true);

        PolicyCalculateVo response = null;
        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, CALCULATE_SERVICE_URL);
        try {
            R r = PolicyUtils.calculate(policyRequest, config);
            if(r.getSuccess()){
                response = (PolicyCalculateVo) r.getContent();
//                Gson gson = new Gson();
//                String str = gson.toJson(response);
//                System.out.println(str);
            }else{
                System.out.println(r.getMessage());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(response);
    }
    @Test
    public void testUpdate() throws MalformedURLException, URISyntaxException {

        //构建一个测试投保单
        PolicyRequest policyRequest = new PolicyRequest();

        //设置产品编码
        //跨境大货创新类保险服务
        policyRequest.setProductCode("1");

        //被保险人
        policyRequest.setInsuredName("宁波测试有限公司");
        //设置原单号
        policyRequest.setTrackingNo(TRACKING_NO);

        //运输方式
        policyRequest.setTransportModeCode(TransportMode.SEA.getCode());
        //派送方式
        policyRequest.setDeliverywayCode(Deliveryway.EXPRESS.getCode());
        //运输工具及航次
        policyRequest.setTransportTool("LH9999");
        //货物描述
        policyRequest.setCargoDesc("led灯1\nled灯2\nled灯3");
        //货物类别
        policyRequest.setCargoCategoryCode(CargoCategory.NORMAL.getCode());
        //起运国
        policyRequest.setDepartureCountryCode("CHN");
        //起运地
        policyRequest.setDepartureAddress("SHENZHEN");
        //目的国
        policyRequest.setDestinationCountryCode("USA");
        //目的地
        policyRequest.setDestinationAddress("MEM 13292 E Holmes Rd  QWQ");
        //起运日期
        policyRequest.setDepartureDate(new Date());
        //快递公司名，如果有快递单号，则快递公司必填
        policyRequest.setExpressCompanyCode(ExpressCompany.UPS.getCode());
        //海外派送快递单号
        policyRequest.setExpressNo("1Z9474VX6832389526QWQ");
        //提单号
        policyRequest.setBlNo("020-48236661QWQ");
        //包装类型 002--纸箱 CARTON
        policyRequest.setPackingCode(Packing.CARTON.getCode());
        //包装数量
        policyRequest.setPackingQuantity(5);
        //公斤数（毛重)
        policyRequest.setChargeableWeight(new BigDecimal(100));
        //货值
        policyRequest.setCargoValue(new BigDecimal(999));
        // 运费
        policyRequest.setFreight(new BigDecimal(100));
        //计费方式：01--货值*加成比例
        policyRequest.setBaseAmountWay(BaseAmountWay.VALUE_AND_FREIGHT.getCode());
        //保单币种
        policyRequest.setCurrencyCode(Currency.USD.getCode());
        //投保比例 %
        policyRequest.setRatio(new BigDecimal(100));
        //ShipmentID
        policyRequest.setShipmentId("FBA092019293939QWQ");

        //备注
        policyRequest.setRemark("NONEQWQ");
        //是否保上架
        // true ：是
        policyRequest.setShelf(true);
        //上架目的地；
        //目前仅仅支持持FBA上架保障；
        policyRequest.setShelfName("FBA");
        //目的地类型
        policyRequest.setDestType(DestType.FBA.getCode());

        // 用户名(登录跨境堡的用户名)
        policyRequest.setCreateUserName("ruby");

        // 是否暴动保障
        policyRequest.setUnrestRisk(true);

        //上传附件 FILE
        PolicyAttachment paq = new PolicyAttachment();
        paq.setRemake("nothing");
        paq.setType("Application");
        paq.setUrl(fileUrl);
        List<PolicyAttachment> list = new ArrayList<>();
        list.add(paq);
        policyRequest.setAttachmentList(list);


        PolicyInsured insured = new PolicyInsured();
        insured.setCertificateType(CertificateType.PID.getCode());
        insured.setCertificateNo("330227100000000002");
        policyRequest.setInsuredInfo(insured);
//
//        List<PolicyAttachment> list = new ArrayList<>();
//
//        list.add()
//        policyRequest.setAttachmentOpenDTOList();
//

        PolicyResponseVO response = null;
        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, UPDATE_SERVICE_URL);
        try {
            R r = PolicyUtils.create(policyRequest, config);
            if(r.getSuccess()){
                response = (PolicyResponseVO) r.getContent();
                System.out.println(r.getMessage());
            }else{
                System.out.println(r.getMessage());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(response);
    }
    @Test
    public void testPostModify() throws MalformedURLException, URISyntaxException, ParseException {

        //构建一个测试投保单
        PolicyRequest policyRequest = new PolicyRequest();

        //设置产品编码
        //跨境大货创新类保险服务
        policyRequest.setProductCode("1");

        //被保险人
        policyRequest.setInsuredName("宁波测试有限公司22");
        //设置原单号
        policyRequest.setTrackingNo(TRACKING_NO);

        //运输方式
        policyRequest.setTransportModeCode(TransportMode.SEA.getCode());
        //派送方式
        policyRequest.setDeliverywayCode(Deliveryway.EXPRESS.getCode());
        //运输工具及航次
        policyRequest.setTransportTool("LH721");
        //货物描述
        policyRequest.setCargoDesc("led灯1");
        //货物类别
        policyRequest.setCargoCategoryCode(CargoCategory.NORMAL.getCode());
        //起运国
        policyRequest.setDepartureCountryCode("CHN");
        //起运地
        policyRequest.setDepartureAddress("SHENZHEN");
        //目的国
        policyRequest.setDestinationCountryCode("USA");
        //目的地
        policyRequest.setDestinationAddress("MEM 13292 E Holmes Rd");
        //起运日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        policyRequest.setDepartureDate(dateFormat.parse("2020-08-20"));
        //快递公司名，如果有快递单号，则快递公司必填
        policyRequest.setExpressCompanyCode(ExpressCompany.UPS.getCode());
        //海外派送快递单号
        policyRequest.setExpressNo("1Z9474VX6832389526");
        //提单号
        policyRequest.setBlNo("020-48236661");
        //包装类型 002--纸箱 CARTON
        policyRequest.setPackingCode(Packing.CARTON.getCode());
        //包装数量
        policyRequest.setPackingQuantity(5);
        //公斤数（毛重)
        policyRequest.setChargeableWeight(new BigDecimal(400));
        //货值
        policyRequest.setCargoValue(new BigDecimal(10000));
        // 运费
        policyRequest.setFreight(new BigDecimal(100));
        policyRequest.setFreightCurrencyCode(Currency.CNY.getCode());
        //计费方式：01--货值*加成比例
        policyRequest.setBaseAmountWay(BaseAmountWay.VALUE_AND_FREIGHT.getCode());
        //保单币种
        policyRequest.setCurrencyCode(Currency.USD.getCode());
        //投保比例 %
        policyRequest.setRatio(new BigDecimal(100));
        //ShipmentID
        policyRequest.setShipmentId("FBA092019293939");

        //备注
        policyRequest.setRemark("NONE");
        //是否保上架
        // true ：是
        policyRequest.setShelf(true);
        //上架目的地；
        //目前仅仅支持持FBA上架保障；
        policyRequest.setShelfName("FBA");
        //目的地类型
        policyRequest.setDestType(DestType.FBA.getCode());

        // 用户名(登录跨境堡的用户名)
        policyRequest.setCreateUserName("ruby");

        // 是否暴动保障
        policyRequest.setUnrestRisk(true);

        policyRequest.setAmendmentReason("批改申请理由~~~");

        //上传附件 FILE
        PolicyAttachment paq = new PolicyAttachment();
//        URI uri = new URI(fileUrl);
//        File file = new File(fileUrl);
        paq.setRemake("nothing");
        paq.setType("Application");
        paq.setUrl(fileUrl);
        PolicyAttachment paq2 = new PolicyAttachment();
        paq2.setRemake("无货损保函");
        paq2.setType("guarantee_letter");
        paq2.setUrl(fileUrl);
        List<PolicyAttachment> list = new ArrayList<>();
        list.add(paq);
        list.add(paq2);
        policyRequest.setAttachmentList(list);


        PolicyInsured insured = new PolicyInsured();
        insured.setCertificateType(CertificateType.PID.getCode());
        insured.setCertificateNo("330227100000000002");
        policyRequest.setInsuredInfo(insured);
//
//        List<PolicyAttachment> list = new ArrayList<>();
//
//        list.add()
//        policyRequest.setAttachmentOpenDTOList();
//

        PolicyResponseVO response = null;
        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, POST_MODIFY_SERVICE_URL);
        try {
            R r = PolicyUtils.create(policyRequest, config);
            if(r.getSuccess()){
                response = (PolicyResponseVO) r.getContent();
                System.out.println(r.getMessage());
            }else{
                System.out.println(r);
                System.out.println(r.getMessage());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(response);
    }

    /**
     * 测试新增的投保单是否存在，并且可以获取整个投保单的信息；
     * @return
     */
//    @Test
    public PolicyResponseVO testQuery(){

        PolicyResponseVO response = null;
        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, QUERY_SERVICE_URL);

        PolicyRequest policyRequest = new PolicyRequest();
        policyRequest.setTrackingNo(TRACKING_NO);


        try {
            R r = PolicyUtils.query(policyRequest, config);
            if(r.getSuccess()){
                response = (PolicyResponseVO) r.getContent();

                System.out.println(response);
//                System.out.println("原单号为："+response.getTrackingNo()+"的投保单已经存在！");
            }else{
                System.out.println(r.getMessage());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(response);
        return response;
    }


}
